package cn.mjnxin.iot.base.util;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * 自定义类加载器.<br/>
 *
 * @author CLG 2023年10月10日
 * @version V1.0.0
 */
public class EngineClassLoader extends URLClassLoader {
	public EngineClassLoader() {
//        this(getSystemClassLoader());
		this(Thread.currentThread().getContextClassLoader());
	}

	public EngineClassLoader(ClassLoader parent) {
		super(new URL[] {}, parent);
	}

	public void addURL(URL... urls) {
		if (urls != null) {
			for (URL url : urls) {
				super.addURL(url);
			}
		}
	}

	public void addFile(File... files) throws IOException {
		if (files != null) {
			for (File file : files) {
				if (file != null) {
					super.addURL(file.toURI().toURL());
				}
			}
		}
	}

	@SuppressWarnings("resource")
	public static void main(String[] args) throws Throwable {
		String ZIP_PATH = "E:\\workspace\\test.jar";

		EngineClassLoader loader1 = new EngineClassLoader();
		loader1.addURL(new File(ZIP_PATH).toURI().toURL());
		Class<?> clazz1 = loader1.loadClass("cn.mjnxin.coder.test.AbcSchemaBuilder");

		EngineClassLoader loader2 = new EngineClassLoader();
		loader2.addURL(new File(ZIP_PATH).toURI().toURL());
		Class<?> clazz2 = loader2.loadClass("cn.mjnxin.coder.test.IcbcSchemaBuilder");
		// 相同类加载器的两个不同实例加载的类型不同？
		System.out.println(clazz1 == clazz2);
	}
}